package com.jqr.activity.mycoroutine

import android.annotation.SuppressLint
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.jqr.activity.mycoroutine.api.userServiceApi
import com.jqr.activity.mycoroutine.models.Articles

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main1)

        val nameTextView=findViewById<TextView>(R.id.textView);
        nameTextView.text="hello world"

        val  btnClick=findViewById<Button>(R.id.btnClick).also {

            it.setOnClickListener{

                object :AsyncTask<Void,Void,Articles?>(){
                    @SuppressLint("StaticFieldLeak")
                    override fun onPostExecute(result: Articles?) {
                        super.onPostExecute(result)
                        nameTextView.text=result?.msg
                    }
                    @SuppressLint("StaticFieldLeak")
                    override fun doInBackground(vararg params: Void?): Articles? {
                       return userServiceApi.getArticles().execute().body()
                    }

                }.execute()
            }
        }
    }
}